//
//  CMMyHelpCtrl.m
//  CreditManger
//
//  Created by chaiyanan on 15/11/2.
//  Copyright © 2015年 jinxin. All rights reserved.
//

#import "CMMyHelpCtrl.h"
#import "CMLoginManager.h"
#import "CMGetTitlesInfo.h"
#import "CMGetTitlesItemInfo.h"
#import "CMGetTitlesParam.h"
#import "CMGetTitlesParser.h"

@interface CMMyHelpCtrl ()
<
UITableViewDataSource,
UITableViewDelegate
>

@property (nonatomic, weak) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSArray *dataAry;

@end

@implementation CMMyHelpCtrl

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = [LCCommon getColor:Main_BackGround_Color];
    // Do any additional setup after loading the view.
    
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 44.0f;
    
    [self requestToGetHelpTitles];
    
//    self.dataAry = @[@"什么是“个人信用报告”？",
//                     @"什么事“信用报告的查询时间”，“信用报告的生成时间”？",
//                     @"什么是“账户数”",
//                     @"什么事“信用报告的查询时间”，“信用报告的生成时间”？什么事“信用报告的查询时间”，“信用报告的生成时间”？",
//                     @"什么事“信用报告的查询时间”，“信用报告的生成时间”？什么事“信用报告的查询时间”，“信用报告的生成时间”？什么事“信用报告的查询时间”，“信用报告的生成时间”？什么事“信用报告的查询时间”，“信用报告的生成时间”？"];
    
    
//    self.tableView.rowHeight = UITableViewAutomaticDimension;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - tableView dataSource/delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataAry.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HelpListCell" forIndexPath:indexPath];
    UILabel *label = (UILabel *)[cell.contentView viewWithTag:101];
//    label.text = self.dataAry[indexPath.row];
    CMGetTitlesItemInfo *itemInfo = self.dataAry[indexPath.row];
    label.text = itemInfo.title;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
    headerView.backgroundColor = [UIColor clearColor];
    return headerView;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.5)];
    headerView.backgroundColor = [UIColor clearColor];
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.5;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HelpListCell"];
//    UILabel *label = (UILabel *)[cell.contentView viewWithTag:101];
//    label.text = self.dataAry[indexPath.row];
//    [cell setNeedsUpdateConstraints];
//    CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
//    CGFloat height = size.height+1;
//    return height;
//}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - net method

- (void)requestToGetHelpTitles
{
    CMGetTitlesParam *param = [[CMGetTitlesParam alloc]init];
    param.token = [CMLoginManager sharedCMLoginManager].currentUserInfo.token;
    param.uid = [CMLoginManager sharedCMLoginManager].currentUserInfo.uid;
    
    CMGetTitlesParser *parser = [[CMGetTitlesParser alloc]init];
    [NetCenter getDataFromURL:Method_getTitles params:param parser:parser result:^(ParserResult *myResult) {
        if (myResult.code==0)
        {
            CMGetTitlesInfo *info = (CMGetTitlesInfo *)myResult.result;
            self.dataAry = [NSArray arrayWithArray:info.dataAry];
            [self.tableView reloadData];
        }
        else
        {
            DISSMISS_ERR(myResult.message);
        }
        
    } failure:^(NSError *error) {
        DISSMISS_ERR([error description]);
    }];
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    UITableViewCell *cell = (UITableViewCell *)sender;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    UIViewController *vc = segue.destinationViewController;
    [vc setValue:self.dataAry[indexPath.row] forKey:@"itemInfo"];
    
}


@end
